///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2011 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IO_CFD_H_
#define _BUOLA_IO_CFD_H_

#include <buola/buola.h>
#include <unistd.h>

namespace buola { namespace io {

DECLARE_NAME(OpenShm)
    
class CFD
{
public:
    explicit CFD(int pFD=-1)
        :   mFD(pFD)
    {}
    
    CFD(NOpenShm,const std::string &pName,EMode pMode,EPermission pPerm,bool *pCreated);

    CFD(CFD &&pRH)
        :   mFD(pRH.mFD)
    {
        pRH.mFD=-1;
    }

    void Close();
    void Sync(bool pFull=true);
    CFD Dup(int pNew,EMode pMode=EMode::NONE);

    void Truncate(std::size_t pSize);
    std::size_t GetSize();
    
    void *Map(EMode pMode,off_t pOffset,std::size_t pSize,void *pHint);
    static void Unmap(void *pAddress,std::size_t pSize);
    
    void Reset(int pFD=-1)      {   mFD=pFD;    }
    int Get() {   return mFD; }

    explicit operator bool()    {   return mFD!=-1;     }
    bool operator!()            {   return mFD==-1;     }

    static size_t GetPageSize();

private:
    int mFD;
};

/*namespace io*/ } /*namespace buola*/ }

#endif
